package com.qf.hospital.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.hospital.constants.HospitalConstants;
import com.qf.hospital.controller.CaoController;
import com.qf.hospital.entity.Cashier;
import com.qf.hospital.entity.Pharmacy;
import com.qf.hospital.entity.PharmacyExample;
import com.qf.hospital.enums.HospitalExceptionEnum;
import com.qf.hospital.exception.HosptialException;
import com.qf.hospital.mapper.PharmacyMapper;
import com.qf.hospital.service.CashierService;
import com.qf.hospital.service.PharmacyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 何星宇
 * @version V1.0
 * @Date 2021/10/21 14:54
 */

@Service
public class PharmacyServiceImpl implements PharmacyService {
    @Autowired
    private PharmacyMapper mapper;
    private Logger log = LoggerFactory.getLogger(CaoController.class);
    @Autowired
    private CashierService cashierService;

    /**
     * 根据条件查询药品
     * @param drugname
     * @param page
     * @param limit
     * @return
     */
    @Override
    public PageInfo<Pharmacy> findByPharmacyByDrugNameAndPage(String drugname, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Pharmacy> pharmacyList = mapper.findPharmacyByDrugName(drugname);
        PageInfo<Pharmacy> pageInfo = new PageInfo<>(pharmacyList);
        return pageInfo;
    }

    /**
     * 根据药品名称查询药品（可以开启悲观锁，但是主要索引是否生效问题）
     * @param pharmacyName
     * @return
     */
    @Override
    public Pharmacy findPharmacyName(String pharmacyName) {
        Pharmacy pharmacy = mapper.findPharmacyName(pharmacyName);
        return pharmacy;
    }

    /**
     * 修改药品信息
     * @param pharmacy
     */
    @Override
    public void updateByPharmacyName(Pharmacy pharmacy) {
        PharmacyExample example = new PharmacyExample();
        example.createCriteria().andPharmacyNameEqualTo(pharmacy.getPharmacyName());
        int count = mapper.updateByExampleSelective(pharmacy, example);
        if (count!=1){
            //记录日志
            log.info("【系统异常】 修改药品信息失败");
            throw new HosptialException(HospitalExceptionEnum.SYSTEM_EXCEPTION,"修改药品信息失败");
        }
    }



    /**
     * 修改处方信息
     * @param reportId 患者id
     * @param pharmacyName 药品名称
     * @param num 药品数量
     */
    @Override
    @Transactional
    public void updatePrescription(Integer reportId, String pharmacyName, Integer num) {
        //1. 查询当前药品
        Pharmacy pharmacy = findPharmacyName(pharmacyName);

        //2. 判断药品库存数是否充足
        Integer drugStoreNum = pharmacy.getDrugStoreNum();
        if (num>pharmacy.getDrugStoreNum()){
            //记录日志
            log.info("【给患者添加/修改处方信息】 当前药品库存不足");
            //抛出异常
            throw new HosptialException(HospitalExceptionEnum.INVALID_PARAMETER,"当前药品库存不足");
        }

        //3. 扣减药品的库存数
        Pharmacy newPharmacy = new Pharmacy();
        newPharmacy.setPharmacyName(pharmacyName);
        newPharmacy.setDrugStoreNum(drugStoreNum-num);
        updateByPharmacyName(newPharmacy);

        //4. 查询药品单价，计算总价
        Double sellingPrice = pharmacy.getSellingPrice();
        Double totalPrice = sellingPrice*num;
        //5. 查询患者是否开过当前处方
        Integer result = cashierService.checkPaymentStatusByReportIdAndDrugName(reportId,pharmacyName);
        //6. 开过修改，没开过，就添加
        if (result==0){
            Cashier cashier = new Cashier();
            cashier.setReportId(reportId);
            cashier.setDurgName(pharmacyName);
            cashier.setDurgNum(num);
            cashier.setRepicePrice(sellingPrice);
            cashier.setRepiceTotal(totalPrice);
            cashier.setState(HospitalConstants.CASHIER_STATE_DRUG);
            cashier.setCtime(new Date());
            cashier.setOstate(HospitalConstants.CASHIER_PROJECT_TYPE_NOT_EXAMINE);
            cashier.setMstate(HospitalConstants.CASHIER_PAYMENT_STATUS_NO);
            //添加
            cashierService.addCashier(cashier);
        }else {
            //修改
            cashierService.updateCashier(reportId,pharmacyName,num,totalPrice);
        }

    }

    /**
     * 修改药品库存
     * @param pharmacyName
     * @param pharmacyNum
     */
    @Override
    public void addInventoryByPharmacyName(String pharmacyName, Integer pharmacyNum) {
        Integer count = mapper.addInventoryByPharmacyName(pharmacyName,pharmacyNum);
        if (count!=1){
            //记录日志
            log.info("【系统异常】 修改药品库存失败");
            throw new HosptialException(HospitalExceptionEnum.SYSTEM_EXCEPTION,"修改药品库存失败");
        }
    }
}
